#include <libdswav/dswosd.h>
#include "font_nosignal.h"

#define YUV_WIDTH (19 * 24)
#define YUV_HEIGHT (18 * 2)
#define YUV_BUFSIZE (YUV_WIDTH * YUV_HEIGHT * 2)

#define TEXT_OSD_INDEX_BASE (13 * 4)
#define CH_TXT_SIZE (16 * 4)
#define MAX_TIM_WIN 3
#define TIM_WIN_INDEX_BASE 0
#define TIM_X_BASE(X) (X - 10 - YUV_WIDTH)
#define MAX_TXT_WIN 3
#define TXT_WIN_INDEX_BASE (TIM_WIN_INDEX_BASE + MAX_TIM_WIN)
#define MAX_PALETTE 2
#define NOSIGNAL_WIN_INDEX (TXT_WIN_INDEX_BASE + MAX_TXT_WIN)
#define MAX_NOSIGNAL_WIN 2

#define MK_TXTOSD_INDEX_BASE(x, y) (x + y * CH_TXT_SIZE)

#define FONT_SPACE_INDEX_BASE 48

CDswosd::CDswosd(LPST_ENV_T pEnv, void* pCapture, void* pBindfd, int nCh, gm_palette_table_t *pPalette)
: CDswbase(pEnv)
, m_pCapture(pCapture)
, m_pBindfd(pBindfd)
, m_nChId(nCh)
, m_nOldTime(0)
, m_bShowTm(false)
, m_bShowTxt(false)
, m_pYUVBuffer(NULL)
, m_pFontCvt(NULL)
, m_pPalette(pPalette)
, m_pNoSignalFont(NULL)
{
	LOG4CPLUS_DEBUG(log_dsw, "Create OSD on channel " << nCh << "!");
	ST_VIDEO_PARAM nParam;

	pthread_mutex_init(&m_nSignal.nLock, NULL);
	m_nSignal.nMask = 0;
	if (nCh == 0)
		CreateNoSignalOSD();
	m_pNoSignalFont = new unsigned short[NOSIGNAL_FONT_NUM * 4 * 4];
	ShowNoSignalOSD(true);

	SendMessage(PMSG_GET_VIDEO_PARAM, (void*)&nParam);
	strcpy(m_sOsdText, nParam.nVParam[nCh].nOsd.sosd);
	m_bShowTm = nParam.nVParam[nCh].nOsd.enosd_time;
	m_bShowTxt = nParam.nVParam[nCh].nOsd.enosd_txt;
	m_pFontCvt = new CDswfontcvt(pEnv);

//	m_bShowTm = m_bShowTxt = false;

	UpdateOsdFont((char*)"0123456789-: ", true);
	if (strlen(nParam.nVParam[nCh].nOsd.sosd) <= 0)
		nParam.nVParam[nCh].nOsd.enosd_txt = false;
	if (nParam.nVParam[nCh].nOsd.enosd_txt)
		UpdateOsdFont(m_sOsdText, false);
	ShowTextOsd(m_sOsdText, m_bShowTxt);

	memset(m_sLastWin0, 0, sizeof(m_sLastWin0));
	memset(m_sLastWin1, 0, sizeof(m_sLastWin1));
	memset(m_sLastWin2, 0, sizeof(m_sLastWin2));
	m_pYUVBuffer = new char[YUV_BUFSIZE];
	CREATE_EV(m_nOsdTask, 0, 1000 * 500, pEnv->pEvBase, CDswosd::cbOsdTask, this);
	CREATE_EV(m_nOsdPaletteTask, 5, 0, pEnv->pEvBase, CDswosd::cbUpdateOSDPaletteTask, this);
}

CDswosd::~CDswosd()
{
	CLEAR_EV(m_nOsdTask);
	CLEAR_EV(m_nOsdPaletteTask);
	delete m_pYUVBuffer;
	if (m_pFontCvt)
		delete m_pFontCvt;
	if (m_pNoSignalFont)
		delete m_pNoSignalFont;
	pthread_mutex_destroy(&m_nSignal.nLock);
	LOG4CPLUS_DEBUG(log_dsw, "Destroy OSD on channel " << m_nChId << "!");
}

int CDswosd::UpdateOsdFont(char* pOsdText, bool bTimeOsd)
{
	int nOsdCount;
	nOsdCount = m_pFontCvt->MakeOsdFont(pOsdText, bTimeOsd ? 0 : MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId));
	return nOsdCount;
}

void CDswosd::OsdTask(int fd, short events)
{
	time_t now = time(NULL);
	if (m_nOldTime != now){
		UpdateTimeOsd(now, m_bShowTm);
		m_nOldTime = now;
		CheckSignal();
	}
	TRIG_EV(m_nOsdTask, 0, 1000 * 500);
}

void CDswosd::CheckSignal()
{
	bool bChange = false;
	bool bNeedDispNoSignal = false;
	MUTEX_LOCK(&m_nSignal.nLock);
	if (m_nSignal.nMask){
		bChange = true;
		if (m_nSignal.nSignal_local[m_nChId] == VIDEO_PRESENT)
			bNeedDispNoSignal = false;
		else
			bNeedDispNoSignal = true;
		m_nSignal.nMask = 0;
	}
	MUTEX_UNLOCK(&m_nSignal.nLock);
	if (bChange)
		ShowNoSignalOSD(bNeedDispNoSignal);
}

bool CDswosd::GetBackgroundYUV(int nX, int nY, int &nYUV_F, int &nYUV_B)
{
	int nRet;
	region_rawdata_t region_rawdata;
	region_rawdata.region.x = nX; //Don't care this element in GM_WHOLE_FROM_ENC_OBJ mode
	region_rawdata.region.y = nY; //Don't care this element in GM_WHOLE_FROM_ENC_OBJ mode
	region_rawdata.region.width = YUV_WIDTH;
	region_rawdata.region.height = YUV_HEIGHT;
	region_rawdata.yuv_buf = m_pYUVBuffer;	//UYUV UYUV
	region_rawdata.yuv_buf_len = YUV_BUFSIZE;
	region_rawdata.rawdata_mode = GM_PARTIAL_FROM_CAP_OBJ;
	nRet = gm_get_rawdata(m_pBindfd, &region_rawdata, 500);
	if (nRet == 0){
		LOG4CPLUS_WARN(log_dsw, "gm_get_rawdata timeout!");
		return false;
	}
	if (nRet == -1){
		LOG4CPLUS_WARN(log_dsw, "yuv dim is over spec!");
		return false;
	}
	int nYvalue = 0;
	for (int i = 1; i < YUV_BUFSIZE; i += 4){
		nYvalue += m_pYUVBuffer[i];
	}
	int nNewY = (unsigned char)(nYvalue / (YUV_HEIGHT * YUV_WIDTH / 2));
	unsigned char nNewY_F = (nNewY > 128) ? 0 : 255;
	unsigned char nNewY_B = (nNewY <= 128) ? 0 : 255;
	nYUV_F = (nNewY_F << 24) | (0x80 << 16) | (nNewY_F << 8) | 0x80;
	nYUV_B = (nNewY_B << 24) | (0x80 << 16) | (nNewY_B << 8) | 0x80;
	return true;
}

void CDswosd::UpdateOSDPaletteTask(int fd, short events)
{
	int nYUV_F, nYUV_B;
	if (GetBackgroundYUV((defRESOLUTIONS[0].width - 10 - YUV_WIDTH) + ((1280 - 10 - YUV_WIDTH) % 4), 10, nYUV_F, nYUV_B)){
		m_pPalette->palette_table[MAX_PALETTE * m_nChId + 0] = nYUV_B;
		m_pPalette->palette_table[MAX_PALETTE * m_nChId + 1] = nYUV_F;
		int ret = gm_set_palette_table(m_pPalette);
		if (ret < 0) {
			printf("Set osd palette failed\n");
		}
	}
	TRIG_EV(m_nOsdPaletteTask, 5, 0);
}

void CDswosd::UpdateTimeOsd(time_t nNow, bool bShow)
{
	char curtime[64];
	struct tm tm_ret;
	localtime_r(&nNow, &tm_ret);
	gm_osd_font_t osd_font;
	int i;

	if (!bShow){
		osd_font.enabled = 0;
		for (i = 0; i < MAX_TIM_WIN; i++){
			osd_font.win_idx = TIM_WIN_INDEX_BASE + i;
			gm_set_osd_font(m_pCapture, &osd_font);
		}
	}
	else{
		osd_font.enabled = 1;
		osd_font.win_idx = TIM_WIN_INDEX_BASE;
		osd_font.align_type = GM_OSD_ALIGN_TOP_LEFT;
		osd_font.y = 10;
		osd_font.v_words = 2;
		osd_font.h_space = 0;
		osd_font.v_space = 0;

		osd_font.font_alpha = GM_OSD_FONT_ALPHA_100;
		osd_font.win_alpha = GM_OSD_FONT_ALPHA_0;
		osd_font.win_palette_idx = MAX_PALETTE * m_nChId + 0;
		osd_font.font_palette_idx = MAX_PALETTE * m_nChId + 1;
		osd_font.priority = GM_OSD_PRIORITY_MARK_ON_OSD;
		osd_font.smooth.enabled = 1;
		osd_font.smooth.level = GM_OSD_FONT_SMOOTH_LEVEL_STRONG;
		osd_font.marquee.mode = GM_OSD_MARQUEE_MODE_NONE;
		osd_font.border.enabled = 0;
		osd_font.border.width = 1;
		osd_font.border.type = GM_OSD_BORDER_TYPE_WIN;
		osd_font.border.palette_idx = MAX_PALETTE * m_nChId + 0;
		osd_font.font_zoom = GM_OSD_FONT_ZOOM_NONE;

		strftime(curtime, sizeof(curtime), "%Y-%m", &tm_ret);
		if (strcmp(curtime, m_sLastWin0) != 0){
			strcpy(m_sLastWin0, curtime);
			osd_font.x = TIM_X_BASE(defRESOLUTIONS[0].width);
			osd_font.h_words = 7 * 2;
			osd_font.font_index_len = osd_font.h_words * 2;
			for (i = 0; i < strlen(curtime); i++){
				if (curtime[i] == '-'){
					osd_font.font_index[i * 2] = 40;
					osd_font.font_index[i * 2 + 1] = 41;
					osd_font.font_index[i * 2 + osd_font.h_words] = 42;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 43;
				}
				else if (curtime[i] == ':'){
					osd_font.font_index[i * 2] = 44;
					osd_font.font_index[i * 2 + 1] = 45;
					osd_font.font_index[i * 2 + osd_font.h_words] = 46;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 47;
				}
				else if (curtime[i] == ' '){
					osd_font.font_index[i * 2] = 48;
					osd_font.font_index[i * 2 + 1] = 49;
					osd_font.font_index[i * 2 + osd_font.h_words] = 50;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 51;
				}
				else{
					osd_font.font_index[i * 2] = (curtime[i] - 0x30) * 4;
					osd_font.font_index[i * 2 + 1] = (curtime[i] - 0x30) * 4 + 1;
					osd_font.font_index[i * 2 + osd_font.h_words] = (curtime[i] - 0x30) * 4 + 2;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = (curtime[i] - 0x30) * 4 + 3;
				}
			}
			gm_set_osd_font(m_pCapture, &osd_font);
		}
		strftime(curtime, sizeof(curtime), "-%d %H:", &tm_ret);
		if (strcmp(curtime, m_sLastWin1) != 0){
			strcpy(m_sLastWin1, curtime);
			osd_font.win_idx = TIM_WIN_INDEX_BASE + 1;
			osd_font.x = TIM_X_BASE(defRESOLUTIONS[0].width) + 7 * 24;
			osd_font.h_words = 7 * 2;
			osd_font.font_index_len = osd_font.h_words * 2;
			for (i = 0; i < strlen(curtime); i++){
				if (curtime[i] == '-'){
					osd_font.font_index[i * 2] = 40;
					osd_font.font_index[i * 2 + 1] = 41;
					osd_font.font_index[i * 2 + osd_font.h_words] = 42;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 43;
				}
				else if (curtime[i] == ':'){
					osd_font.font_index[i * 2] = 44;
					osd_font.font_index[i * 2 + 1] = 45;
					osd_font.font_index[i * 2 + osd_font.h_words] = 46;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 47;
				}
				else if (curtime[i] == ' '){
					osd_font.font_index[i * 2] = 48;
					osd_font.font_index[i * 2 + 1] = 49;
					osd_font.font_index[i * 2 + osd_font.h_words] = 50;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 51;
				}
				else{
					osd_font.font_index[i * 2] = (curtime[i] - 0x30) * 4;
					osd_font.font_index[i * 2 + 1] = (curtime[i] - 0x30) * 4 + 1;
					osd_font.font_index[i * 2 + osd_font.h_words] = (curtime[i] - 0x30) * 4 + 2;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = (curtime[i] - 0x30) * 4 + 3;
				}
			}
			gm_set_osd_font(m_pCapture, &osd_font);
		}
		strftime(curtime, sizeof(curtime), "%M:%S", &tm_ret);
		if (strcmp(curtime, m_sLastWin2) != 0){
			strcpy(m_sLastWin2, curtime);
			osd_font.win_idx = TIM_WIN_INDEX_BASE + 2;
			osd_font.x = TIM_X_BASE(defRESOLUTIONS[0].width) + 7 * 24 + 7 * 24;
			osd_font.h_words = 5 * 2;
			osd_font.font_index_len = osd_font.h_words * 2;
			for (i = 0; i < strlen(curtime); i++){
				if (curtime[i] == '-'){
					osd_font.font_index[i * 2] = 40;
					osd_font.font_index[i * 2 + 1] = 41;
					osd_font.font_index[i * 2 + osd_font.h_words] = 42;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 43;
				}
				else if (curtime[i] == ':'){
					osd_font.font_index[i * 2] = 44;
					osd_font.font_index[i * 2 + 1] = 45;
					osd_font.font_index[i * 2 + osd_font.h_words] = 46;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 47;
				}
				else if (curtime[i] == ' '){
					osd_font.font_index[i * 2] = 48;
					osd_font.font_index[i * 2 + 1] = 49;
					osd_font.font_index[i * 2 + osd_font.h_words] = 50;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = 51;
				}
				else{
					osd_font.font_index[i * 2] = (curtime[i] - 0x30) * 4;
					osd_font.font_index[i * 2 + 1] = (curtime[i] - 0x30) * 4 + 1;
					osd_font.font_index[i * 2 + osd_font.h_words] = (curtime[i] - 0x30) * 4 + 2;
					osd_font.font_index[i * 2 + osd_font.h_words + 1] = (curtime[i] - 0x30) * 4 + 3;
				}
			}
			gm_set_osd_font(m_pCapture, &osd_font);
		}
	}
}

void CDswosd::ShowTextOsd(char* pText, bool bShow)
{
	gm_osd_font_t osd_font[MAX_TXT_WIN];
	int i, j, nFontRamIndex, nTextCount;
	if (!bShow || (strlen(pText) <= 0)){
		for (i = 0; i < MAX_TXT_WIN; i++){
			osd_font[i].enabled = 1;
			osd_font[i].align_type = GM_OSD_ALIGN_TOP_LEFT;
			osd_font[i].win_idx = TXT_WIN_INDEX_BASE + i;
			osd_font[i].x = 10 + i * 7 * 24;
			osd_font[i].y = 10;
			osd_font[i].v_words = 2;
			osd_font[i].h_space = 0;
			osd_font[i].v_space = 0;
			if (i != (MAX_TXT_WIN - 1))
				osd_font[i].h_words = 7 * 2;
			else
				osd_font[i].h_words = 2 * 2;
			osd_font[i].font_index_len = osd_font[i].h_words * 2;
			for (j = 0; j < osd_font[i].h_words; j += 2){
				osd_font[i].font_index[j] = FONT_SPACE_INDEX_BASE;
				osd_font[i].font_index[j + 1] = FONT_SPACE_INDEX_BASE + 1;
				osd_font[i].font_index[j + osd_font[i].h_words] = FONT_SPACE_INDEX_BASE + 2;
				osd_font[i].font_index[j + osd_font[i].h_words + 1] = FONT_SPACE_INDEX_BASE + 3;
			}
			osd_font[i].font_alpha = GM_OSD_FONT_ALPHA_100;
			osd_font[i].win_alpha = GM_OSD_FONT_ALPHA_0;
			osd_font[i].win_palette_idx = MAX_PALETTE * m_nChId + 0;
			osd_font[i].font_palette_idx = MAX_PALETTE * m_nChId + 1;
			osd_font[i].priority = GM_OSD_PRIORITY_MARK_ON_OSD;
			osd_font[i].smooth.enabled = 1;
			osd_font[i].smooth.level = GM_OSD_FONT_SMOOTH_LEVEL_STRONG;
			osd_font[i].marquee.mode = GM_OSD_MARQUEE_MODE_NONE;
			osd_font[i].border.enabled = 0;
			osd_font[i].border.width = 1;
			osd_font[i].border.type = GM_OSD_BORDER_TYPE_WIN;
			osd_font[i].border.palette_idx = MAX_PALETTE * m_nChId + 0;
			osd_font[i].font_zoom = GM_OSD_FONT_ZOOM_NONE;
			gm_set_osd_font(m_pCapture, &osd_font[i]);
		}
		return;
	}
	for (i = 0; i < MAX_TXT_WIN; i++){
		osd_font[i].enabled = 1;
		osd_font[i].align_type = GM_OSD_ALIGN_TOP_LEFT;
		osd_font[i].win_idx = TXT_WIN_INDEX_BASE + i;
		osd_font[i].x = 10 + i * 7 * 24;
		osd_font[i].y = 10;
		osd_font[i].v_words = 2;
		osd_font[i].h_space = 0;
		osd_font[i].v_space = 0;
		if (i != (MAX_TXT_WIN - 1))
			osd_font[i].h_words = 7 * 2;
		else
			osd_font[i].h_words = 2 * 2;
		osd_font[i].font_index_len = osd_font[i].h_words * 2;
		for (j = 0; j < osd_font[i].h_words; j += 2){
			osd_font[i].font_index[j] = FONT_SPACE_INDEX_BASE;
			osd_font[i].font_index[j + 1] = FONT_SPACE_INDEX_BASE + 1;
			osd_font[i].font_index[j + osd_font[i].h_words] = FONT_SPACE_INDEX_BASE + 2;
			osd_font[i].font_index[j + osd_font[i].h_words + 1] = FONT_SPACE_INDEX_BASE + 3;
		}
		osd_font[i].font_alpha = GM_OSD_FONT_ALPHA_100;
		osd_font[i].win_alpha = GM_OSD_FONT_ALPHA_0;
		osd_font[i].win_palette_idx = MAX_PALETTE * m_nChId + 0;
		osd_font[i].font_palette_idx = MAX_PALETTE * m_nChId + 1;
		osd_font[i].priority = GM_OSD_PRIORITY_MARK_ON_OSD;
		osd_font[i].smooth.enabled = 1;
		osd_font[i].smooth.level = GM_OSD_FONT_SMOOTH_LEVEL_STRONG;
		osd_font[i].marquee.mode = GM_OSD_MARQUEE_MODE_NONE;
		osd_font[i].border.enabled = 0;
		osd_font[i].border.width = 1;
		osd_font[i].border.type = GM_OSD_BORDER_TYPE_WIN;
		osd_font[i].border.palette_idx = MAX_PALETTE * m_nChId + 0;
		osd_font[i].font_zoom = GM_OSD_FONT_ZOOM_NONE;
	}
	for (i = 0, nTextCount = 0; i < strlen(pText); i++, nTextCount++){
		if (pText[i] & 0x80)
			i++;
	}
	nFontRamIndex = 0;
	if (nTextCount < 7){
		for (i = 0; i < (nTextCount * 2); i += 2){
			osd_font[0].font_index[i] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId)+ nFontRamIndex++;
			osd_font[0].font_index[i + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + osd_font[0].h_words] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + osd_font[0].h_words + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
		}
	}
	else if ((nTextCount >= 7) && (nTextCount < 14)){
		for (i = 0; i < (7 * 2); i += 2){
			osd_font[0].font_index[i] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + osd_font[0].h_words] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + osd_font[0].h_words + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
		}
		for (i = 0; i < ((nTextCount - 7) * 2); i += 2){
			osd_font[1].font_index[i] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[1].font_index[i + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[1].font_index[i + osd_font[1].h_words] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[1].font_index[i + osd_font[1].h_words + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
		}
	}
	else{
		for (i = 0; i < (7 * 2); i += 2){
			osd_font[0].font_index[i] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + osd_font[0].h_words] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[0].font_index[i + osd_font[0].h_words + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
		}
		for (i = 0; i < (7 * 2); i += 2){
			osd_font[1].font_index[i] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[1].font_index[i + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[1].font_index[i + osd_font[1].h_words] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[1].font_index[i + osd_font[1].h_words + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
		}
		for (i = 0; i < ((((nTextCount - 14) > 2) ? 2 : (nTextCount - 14)) * 2); i += 2){
			osd_font[2].font_index[i] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[2].font_index[i + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[2].font_index[i + osd_font[2].h_words] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
			osd_font[2].font_index[i + osd_font[2].h_words + 1] = MK_TXTOSD_INDEX_BASE(TEXT_OSD_INDEX_BASE, m_nChId) + nFontRamIndex++;
		}
	}
	for (j = 0; j < MAX_TXT_WIN; j++){
		gm_set_osd_font(m_pCapture, &osd_font[j]);
	}
}

void CDswosd::CreateNoSignalOSD()
{
	gm_osd_font_update_t nNewFont;
	int nIndexBase = TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE;
	int nTxtCount, nLine;
	for (nTxtCount = 0; nTxtCount < NOSIGNAL_FONT_NUM; nTxtCount++){
		int x, y, nFontRow, bytesPerLine, totalLine;
		bytesPerLine = ((NOSIGNAL_FONT_W + 7) / 8);
		totalLine = ((NOSIGNAL_FONT_H + (GM_OSD_FONT_MAX_ROW - 1)) / GM_OSD_FONT_MAX_ROW);
#if 0
		printf("------------------------------------------------------------\n");
		for (x = 0; x < NOSIGNAL_FONT_H; x++){
			for (y = 0; y < bytesPerLine; y++){
				char nbyte= FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + x * bytesPerLine + y];
				for (int i = 0; i < 8; i++){
					if (nbyte & 0x80)
						printf("*");
					else
						printf(" ");
					nbyte <<= 1;
				}
			}
			printf("\n");
		}
		printf("------------------------------------------------------------\n");
#endif
		for (x = 0; x < totalLine; x++){
			y = GM_OSD_FONT_MAX_ROW * x;
			nNewFont.font_idx = nIndexBase++;
			for (nFontRow = 0; nFontRow < GM_OSD_FONT_MAX_ROW; nFontRow++, y++){
				if (y < NOSIGNAL_FONT_H)
					nNewFont.bitmap[nFontRow] = ((FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 1] << 8) | (FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 0]));
				else
					nNewFont.bitmap[nFontRow] = 0x0000;
			}
			gm_update_new_font(&nNewFont);
			y = GM_OSD_FONT_MAX_ROW * x;
			nNewFont.font_idx = nIndexBase++;
			for (nFontRow = 0; nFontRow < GM_OSD_FONT_MAX_ROW; nFontRow++, y++){
				if (y < NOSIGNAL_FONT_H)
					nNewFont.bitmap[nFontRow] = ((FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 1] << 4) | (FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 2] >> 4)) | ((FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 2] << 4) << 8);
				else
					nNewFont.bitmap[nFontRow] = 0x0000;
			}
			gm_update_new_font(&nNewFont);
			y = GM_OSD_FONT_MAX_ROW * x;
			nNewFont.font_idx = nIndexBase++;
			for (nFontRow = 0; nFontRow < GM_OSD_FONT_MAX_ROW; nFontRow++, y++){
				if (y < NOSIGNAL_FONT_H)
					nNewFont.bitmap[nFontRow] = ((FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 4] << 8) | (FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 3]));
				else
					nNewFont.bitmap[nFontRow] = 0x0000;
			}
			gm_update_new_font(&nNewFont);
			y = GM_OSD_FONT_MAX_ROW * x;
			nNewFont.font_idx = nIndexBase++;
			for (nFontRow = 0; nFontRow < GM_OSD_FONT_MAX_ROW; nFontRow++, y++){
				if (y < NOSIGNAL_FONT_H)
					nNewFont.bitmap[nFontRow] = ((FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 4] << 4) | (FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 5] >> 4)) | ((FONT_NOSIGNAL[nTxtCount * NOSIGNAL_FONT_SIZE + y * bytesPerLine + 5] << 4) << 8);
				else
					nNewFont.bitmap[nFontRow] = 0x0000;
			}
			gm_update_new_font(&nNewFont);
		}
	}
}

void CDswosd::ShowNoSignalOSD(bool bShow)
{
	gm_osd_font2_t osd_font;
	int i;
	osd_font.font_index = m_pNoSignalFont;
	osd_font.enabled = bShow ? 1 : 0;
	osd_font.win_idx = NOSIGNAL_WIN_INDEX;
	osd_font.align_type = GM_OSD_ALIGN_CENTER;
	osd_font.x = (defRESOLUTIONS[MAIN_STREAM].width - NOSIGNAL_FONT_W * NOSIGNAL_FONT_NUM) / 2;
	osd_font.y = (defRESOLUTIONS[MAIN_STREAM].height - NOSIGNAL_FONT_H) / 2;
	osd_font.v_words = 3;
	osd_font.h_space = 0;
	osd_font.v_space = 0;

	osd_font.font_alpha = GM_OSD_FONT_ALPHA_100;
	osd_font.win_alpha = GM_OSD_FONT_ALPHA_0;
	osd_font.win_palette_idx = MAX_PALETTE * m_nChId + 0;
	osd_font.font_palette_idx = MAX_PALETTE * m_nChId + 1;
	osd_font.priority = GM_OSD_PRIORITY_MARK_ON_OSD;
	osd_font.smooth.enabled = 1;
	osd_font.smooth.level = GM_OSD_FONT_SMOOTH_LEVEL_STRONG;
	osd_font.marquee.mode = GM_OSD_MARQUEE_MODE_NONE;
	osd_font.border.enabled = 0;
	osd_font.border.width = 1;
	osd_font.border.type = GM_OSD_BORDER_TYPE_WIN;
	osd_font.border.palette_idx = MAX_PALETTE * m_nChId + 0;
	osd_font.font_zoom = GM_OSD_FONT_ZOOM_NONE;

	osd_font.h_words = NOSIGNAL_FONT_NUM * 4;
	osd_font.font_index_len = osd_font.h_words * osd_font.v_words;
	for (i = 0; i < NOSIGNAL_FONT_NUM; i++){
		osd_font.font_index[i * 4] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE;
		osd_font.font_index[i * 4 + 1] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 1;
		osd_font.font_index[i * 4 + 2] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 2;
		osd_font.font_index[i * 4 + 3] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 3;
		osd_font.font_index[i * 4 + osd_font.h_words] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 4;
		osd_font.font_index[i * 4 + osd_font.h_words + 1] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 5;
		osd_font.font_index[i * 4 + osd_font.h_words + 2] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 6;
		osd_font.font_index[i * 4 + osd_font.h_words + 3] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 7;
		osd_font.font_index[i * 4 + 2 * osd_font.h_words] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 8;
		osd_font.font_index[i * 4 + 2 * osd_font.h_words + 1] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 9;
		osd_font.font_index[i * 4 + 2* osd_font.h_words + 2] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 10;
		osd_font.font_index[i * 4 + 2 * osd_font.h_words + 3] = i * 12 + TEXT_OSD_INDEX_BASE + MAX_CHANNEL_LOCAL * CH_TXT_SIZE + 11;
	}
	gm_set_osd_font2(m_pCapture, &osd_font);
}

int CDswosd::OnMessage(EN_MSG_TYPE nMsgType, void * pParam)
{
	switch(nMsgType){
	case PMSG_UPDATE_VIDEO_PARAM:
		{
			LPST_VIDEO_PARAM pVideoParam = (LPST_VIDEO_PARAM)pParam;
			m_bShowTm = pVideoParam->nVParam[m_nChId].nOsd.enosd_time;
			if ((strcmp(pVideoParam->nVParam[m_nChId].nOsd.sosd, m_sOsdText) != 0) || (m_bShowTxt != pVideoParam->nVParam[m_nChId].nOsd.enosd_txt)){
				strcpy(m_sOsdText, pVideoParam->nVParam[m_nChId].nOsd.sosd);
				m_bShowTxt = pVideoParam->nVParam[m_nChId].nOsd.enosd_txt;
				if (strlen(pVideoParam->nVParam[m_nChId].nOsd.sosd) <= 0)
					m_bShowTxt = false;
				if (m_bShowTxt)
					UpdateOsdFont(m_sOsdText, false);
				ShowTextOsd(m_sOsdText, m_bShowTxt);
			}
		}
		break;
	case PMSG_SET_SIGNALCHANGE:
		{
			MUTEX_LOCK(&m_nSignal.nLock);
			m_nSignal.nMask = ((LPST_SIGNAL_T)pParam)->nMask;
			memcpy(m_nSignal.nSignal_local, ((LPST_SIGNAL_T)pParam)->nSignal_local, sizeof(m_nSignal.nSignal_local));
			MUTEX_UNLOCK(&m_nSignal.nLock);
		}
		break;
	}
	return 0;
}

